﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sapiens.at.SharePoint.Settings;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Web;
using Sapiens.at.SharePoint.Utils;
using Microsoft.SharePoint.Utilities;
using System.Linq;
using Sapiens.at.SharePoint.Extensions;

namespace Sapiens.at.SharePoint.SQLM.CONTROLTEMPLATES.Sapiens.at.SharePoint.SQLM
{
    [SettingsKey("sqlmsettings")]
    public partial class Settings : SettingsSection
    {
        private string ctypeid = string.Empty;
        public string CTypeID
        {
            get
            {
                if (!string.IsNullOrEmpty(ctypeid)) return ctypeid;
                ctypeid = Request.QueryString["ctype"];
                return ctypeid;
            }
            set
            {
                ctypeid = value;
            }
        }

        /// <summary>
        /// gets the path to the corresponding usercontrol (.ascx) in the controltemplates folder
        /// </summary>
        public override string UserControlPath
        {
            get { return "~/_CONTROLTEMPLATES/" + SP2013.Path + "Sapiens.at.SharePoint.SQLM/Settings" + SP2013.NameEmptyOr15 + ".ascx"; }
        }

        protected CheckBox cbUpdateAllItems = new CheckBox();
        protected void Page_Load(object sender, EventArgs e)
        {
            //Adding
            SQLMReceiverSection Adding = new SQLMReceiverSection();
            Adding = (SQLMReceiverSection)Page.LoadControl(Adding.UserControlPath);
            Controls.Add(Adding);
            Adding.Synchronization = SPEventReceiverSynchronization.Default;
            Adding.ReceiverType = SPEventReceiverType.ItemAdding;

            //Added
            SQLMReceiverSection AddedSync = new SQLMReceiverSection();
            AddedSync = (SQLMReceiverSection)Page.LoadControl(AddedSync.UserControlPath);
            Controls.Add(AddedSync);
            AddedSync.Synchronization = SPEventReceiverSynchronization.Synchronous;
            AddedSync.ReceiverType = SPEventReceiverType.ItemAdded;

            SQLMReceiverSection AddedAsync = new SQLMReceiverSection();
            AddedAsync = (SQLMReceiverSection)Page.LoadControl(AddedAsync.UserControlPath);
            Controls.Add(AddedAsync);
            AddedAsync.Synchronization = SPEventReceiverSynchronization.Asynchronous;
            AddedAsync.ReceiverType = SPEventReceiverType.ItemAdded;

            //Updating
            SQLMReceiverSection Updating = new SQLMReceiverSection();
            Updating = (SQLMReceiverSection)Page.LoadControl(Updating.UserControlPath);
            Controls.Add(Updating);
            Updating.Synchronization = SPEventReceiverSynchronization.Default;
            Updating.ReceiverType = SPEventReceiverType.ItemUpdating;

            //Updated
            SQLMReceiverSection UpdatedSync = new SQLMReceiverSection();
            UpdatedSync = (SQLMReceiverSection)Page.LoadControl(UpdatedSync.UserControlPath);
            Controls.Add(UpdatedSync);
            UpdatedSync.Synchronization = SPEventReceiverSynchronization.Synchronous;
            UpdatedSync.ReceiverType = SPEventReceiverType.ItemUpdated;

            SQLMReceiverSection UpdatedAsync = new SQLMReceiverSection();
            UpdatedAsync = (SQLMReceiverSection)Page.LoadControl(UpdatedAsync.UserControlPath);
            Controls.Add(UpdatedAsync);
            UpdatedAsync.Synchronization = SPEventReceiverSynchronization.Asynchronous;
            UpdatedAsync.ReceiverType = SPEventReceiverType.ItemUpdated;

            //Deleting
            SQLMReceiverSection Deleting = new SQLMReceiverSection();
            Deleting = (SQLMReceiverSection)Page.LoadControl(Deleting.UserControlPath);
            Controls.Add(Deleting);
            Deleting.Synchronization = SPEventReceiverSynchronization.Default;
            Deleting.ReceiverType = SPEventReceiverType.ItemDeleting;

            SQLMUpdateItemsSection UpdateItems = new SQLMUpdateItemsSection();
            UpdateItems = (SQLMUpdateItemsSection)Page.LoadControl(UpdateItems.UserControlPath);
            Controls.Add(UpdateItems);
            UpdateItems.CTypeID = CTypeID;
        }

        /// <summary>
        /// saves the settings
        /// </summary>
        public override void Save()
        {
            Page.Validate();
            if (Page.IsValid)
            {
                try
                {
                    base.Save();
                    foreach (SQLMReceiverSection section in this.FindControls<SQLMReceiverSection>()) section.Save();
                    foreach (SQLMUpdateItemsSection section in this.FindControls<SQLMUpdateItemsSection>()) section.Save();                    
                }
                catch (Exception ex) { ExceptionHandler.Catch(ex, true, SPContext.Current.Site); }
            }
        }

        public string PageToRedirectOnCancel
        {
            get
            {
                return Page.Request.QueryString["Source"] != null ?
                Page.Request.QueryString["Source"] :
                Microsoft.SharePoint.Utilities.SPUrlUtility.CombineUrl(SPContext.Current.Web.ServerRelativeUrl,
                (Page.Request.QueryString["ctype"] != null ? ("/_layouts/" + SP2013.Path + "ManageContentType.aspx?" + Page.Request.QueryString.ToString()) :
                (SPContext.Current.List != null ? ("/_layouts/" + SP2013.Path + "listedit.aspx?List=" + SPContext.Current.List.ID.ToString()) : "/_layouts/" + SP2013.Path + "settings.aspx")));
            }
        }
    }
}
